Skip to content

The sql.js connection keeps its prepared statements - #5

Open
sleeper-luke wants to merge 1 commit into
mainfrom
luke/perf/sqljs-statement-cache
Open

sleeper-luke wants to merge 1 commit into
mainfrom
luke/perf/sqljs-statement-cache

Conversation

@sleeper-luke

Copy link
Copy Markdown
Contributor

Before: openSqlJsConnection, the connection a web app binds its stores to, prepared and freed a statement on every call. An ingest runs one insert per row, so a 12,000-player body compiled the same INSERT 12,000 times, all on the main thread.

After: Each database keeps its prepared statements by SQL, up to 256, freeing the least recently used. A statement is reset after every run, so none is left holding its table, which SQLite needs before it drops or alters one, as a schema rebuild does.

Measured: app-shared's real stores over this connection, with production bodies. JS is the store's parse and toRows; SQL is timed by SQLite's own clock.

partition before: JS + SQL after: JS + SQL
NFL players (12,229 rows) 49 + 154 ms 53 + 129 ms
NFL week 9 stats (2,256 rows) 155 + 232 ms 164 + 141 ms

The SQL step drops 16–39%. That's main-thread time saved on every browser ingest, in app-web today and in sleeper-web once it binds stores.

Tests: four new cases in src/sqljs/sqljs_connection.test.ts: a statement is prepared once across runs; a statement runs again after failing partway; a table can be dropped and rebuilt after statements read it; and the cache stays bounded, freeing what it evicts. yarn test (789 passed), yarn test:prod (771 passed) and yarn typecheck pass locally.

…piles each insert once rather than once per row

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant